<template>
  <div>
    <el-table :data="data" border v-loading="loading" :row-key="rowKey">
      <el-table-column
        :prop="col.name"
        :type="col.type"
        :width="col.width"
        :align="col.align"
        :label="col.title"
        v-for="(col, index) in cols"
        :key="index"
      >
        <template v-slot="{ row, index }" v-if="col.slot">
          <slot :row="row" :index="index" :name="col.slot"> </slot>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      class="page"
      v-if="value"
      @current-change="(v) => change('pagenum', v)"
      @size-change="(v) => change('pagesize', v)"
      :current-page="value.pagenum"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      :page-size="value.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "MyTable",
  methods: {
    /**
     * 分页
     */
    change(key, value) {
      const obj = { ...this.value, [key]: value };
      this.$emit("input", obj);
      this.$emit("change", obj);
    },
  },
  props: {
    /**
     *loading
     */
    loading: Boolean,
    /**
     * 列
     */
    cols: {
      required: true,
      type: Array,
      default: () => {},
    },
    /**
     * 分页对象
     */
    value: Object,
    /**
     * 总条数
     */
    total: Number,
    /**
     * 数据
     */
    data: {
      required: true,
      type: Array,
      default: () => {},
    },
    /**
     * rowkey
     */
    rowKey: {
      type: String,
      default: "id",
    },
  },
  mounted() {},
};
</script>

<style scoped >
.page {
  margin-top: 20px;
}
</style>
